package com.jfinal.enjoy

import com.intellij.codeInsight.completion.*
import com.intellij.codeInsight.lookup.LookupElementBuilder
import com.intellij.patterns.PlatformPatterns
import com.intellij.util.ProcessingContext

class EnjoyCompletionContributor : CompletionContributor() {
    init {
        extend(
            CompletionType.BASIC,
            PlatformPatterns.psiElement()
                .afterLeaf(PlatformPatterns.psiElement().withText("#")), // 只在 # 后触发
            object : CompletionProvider<CompletionParameters>() {
                override fun addCompletions(
                    parameters: CompletionParameters,
                    context: ProcessingContext,
                    result: CompletionResultSet
                ) {
                    result.addElement(
                        PrioritizedLookupElement.withPriority(
                            LookupElementBuilder.create("")
                                .withPresentableText("#")
                                .withTypeText("Enjoy")
                                .withInsertHandler(TemplateInsertHandler("()", 1)),  // 光标放在括号内
                            1000.0
                        )
                    )

                    // #if -> #if()\n#end
                    result.addElement(
                        LookupElementBuilder.create("if")
                            .withPresentableText("#if")
                            .withTypeText("Enjoy")
                            .withInsertHandler(TemplateInsertHandler("if()\n#end", caretOffset = 3))
                    )

                    // #for -> #for()\n#end
                    result.addElement(
                        LookupElementBuilder.create("for")
                            .withPresentableText("#for")
                            .withTypeText("Enjoy")
                            .withInsertHandler(TemplateInsertHandler("for()\n#end", caretOffset = 4))
                    )

                    // #set -> #set()
                    result.addElement(
                        LookupElementBuilder.create("set")
                            .withPresentableText("#set")
                            .withTypeText("Enjoy")
                            .withInsertHandler(TemplateInsertHandler("set()", caretOffset = 4))
                    )

                    // #define -> #define()\n#end
                    result.addElement(
                        LookupElementBuilder.create("define")
                            .withPresentableText("#define")
                            .withTypeText("Enjoy")
                            .withInsertHandler(TemplateInsertHandler("define ()\n#end", caretOffset = 7))
                    )

                    // #switch -> #switch()\n#case \n#default\n#end
                    result.addElement(
                        LookupElementBuilder.create("switch")
                            .withPresentableText("#switch")
                            .withTypeText("Enjoy")
                            .withInsertHandler(
                                TemplateInsertHandler(
                                    "switch()\n#case \n#default\n#end",
                                    caretOffset = 7
                                )
                            )
                    )

                    // 其它简单指令
                    val others = listOf("include", "end", "else", "else if", "case", "default")
                    for (d in others) {
                        result.addElement(
                            LookupElementBuilder.create(d)
                                .withPresentableText("#$d")
                                .withTypeText("Enjoy")
                        )
                    }
                }
            }
        )
    }
}
